﻿package com.zouloux.persp.ra 
{
	import com.zouloux.persp.RaEngine;
	import com.zouloux.utils.ThreeDee;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.objects.DisplayObject3D;
	
	/**
	 * ...
	 * @author ZoulouX
	 */
	public class RaButton extends MovieClip
	{
		// La target 3D à mapper
		protected var _target						:DisplayObject3D		= new DisplayObject3D();
		
		// La camera pour le mapping
		protected var _camera						:Camera3D;
		
		// La durée de séléction pour l'activation
		protected var _timeout						:uint					= 0;
		
		// La couleur de détéction
		protected var _color						:uint					= 0xFFFFFF;
		
		// La confidence
		protected var _confidence					:uint					= 0x22;
		
		
		// Gestion des décalage pour la détéction de la position
		protected var _OffetX						:Number					= 0;
		protected var _OffetY						:Number					= 0;
		protected var _MultiX						:Number					= 1;
		protected var _MultiY						:Number					= 1;
		
		// Le moteur RA
		protected var _raEngine						:RaEngine;
		
		
		// Les getters
		public function get target():DisplayObject3D { return _target; }
		public function get camera():Camera3D { return _camera; }
		public function get timeout():uint { return _timeout; }
		public function get color():uint { return _color; }
		public function get confidence():uint { return _confidence; }
		public function get OffetX():Number { return _OffetX; }
		public function get OffetY():Number { return _OffetY; }
		public function get MultiX():Number { return _MultiX; }
		public function get MultiY():Number { return _MultiY; }
		public function get raEngine():RaEngine { return _raEngine; }
		
		
		// Les setters
		public function set target(value:DisplayObject3D):void 
		{
			_target = value;
		}
		public function set camera(value:Camera3D):void 
		{
			_camera = value;
		}
		public function set timeout(value:uint):void 
		{
			_timeout = value;
		}
		public function set color(value:uint):void 
		{
			_color = value;
		}
		public function set confidence(value:uint):void 
		{
			_confidence = value;
		}
		
		public function set OffetX(value:Number):void 
		{
			_OffetX = value;
		}
		public function set OffetY(value:Number):void 
		{
			_OffetY = value;
		}
		public function set MultiX(value:Number):void 
		{
			_MultiX = value;
		}
		public function set MultiY(value:Number):void 
		{
			_MultiY = value;
		}
		public function set raEngine(value:RaEngine):void 
		{
			_raEngine = value;
		}
		
		
		// Le constructeur
		public function RaButton ()
		{
			
		}
		
		
		// Actualiser l'état
		public function update (event:Event = null):void
		{
			updatePosition();
			sample();
		}
		
		// Actualiser la position
		public function updatePosition ():void
		{
			// Récupérer la position
			var position:Point = ThreeDee.get2DCords(_target, _camera, _OffetX, _OffetY, _MultiX, _MultiY);
			
			//var position:Point = new Point(_target.screen.x, _target.screen.y);
			
			// L'appliquer
			x = -position.x;
			y = position.y / 2;
		}
		
		// Mesurer sur le bitmap
		public function sample ():void
		{
			// Récupérer la couleur du pixel
			var pix:uint = _raEngine.getPixel(x, y);
			
			// Récupérer ses composantes
			var rPix:uint = pix >> 16;
			var gPix:uint = pix >> 8 & 0xFF;
			var bPix:uint = pix & 0xFF;
			
			// Récupérer les composantes de la couleur à détécter
			var rColor:uint = _color >> 16;
			var gColor:uint = _color >> 8 & 0xFF;
			var bColor:uint = _color & 0xFF;
			
			// Vérifier chaque couleur
			var rCond:Boolean = rPix > rColor - _confidence && rPix < rColor + _confidence;
			var gCond:Boolean = gPix > gColor - _confidence && gPix < gColor + _confidence;
			var bCond:Boolean = bPix > bColor - _confidence && bPix < bColor + _confidence;
			
			// On check
			if (rCond && gCond && bCond)
			{
				dispatchEvent(new MouseEvent(MouseEvent.CLICK));
			}
		}
	}
}